package com.plian.system.controller.sys.orgzation;

import com.plian.Tools.CollectionUtil;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.bean.wf.User;
import com.plian.system.helper.wf.SessionHelper;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.service.sys.orgzation.UserGroupRelationService;
import com.plian.system.service.sys.orgzation.WorkFlowGroupService;
import com.plian.system.service.sys.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工作流用户组控制器
 */
@RestController
@RequestMapping("api/group")
public class WorkFlowGroupController {
    @Autowired
    private WorkFlowGroupService workFlowGroupService;

    @Autowired
    private UserGroupRelationService userGroupRelationService;

    @Autowired
    private UserService userService;


    /**
     * 增加用户组
     *
     * @param pd 请求数据
     * @return ObjResult
     */
    @PostMapping("add")
    @Transactional
    @LogOperation("工作流-增加用户组")
    public ObjResult add(@RequestBody PageData pd) {
        workFlowGroupService.add(pd);
        return new ObjResult(CommonCode.SUCCESS);
    }

    /**
     * 删除岗位类型
     *
     * @param pd 请求数据
     * @return ObjResult
     */
    @PostMapping("delete")
    @Transactional
    @LogOperation("工作流-删除岗位类型")
    public ObjResult delete(@RequestBody PageData pd) {
        workFlowGroupService.delete(pd);
        return new ObjResult(CommonCode.SUCCESS);
    }

    /**
     * 更新岗位类型
     *
     * @param pd 请求数据
     * @return ObjResult
     */
    @PostMapping("update")
    @Transactional
    @LogOperation("删除岗位类型-更新岗位类型")
    public ObjResult update(@RequestBody PageData pd) {
         workFlowGroupService.update(pd);
        return new ObjResult(CommonCode.SUCCESS);
    }

    /**
     * 删除用户组
     *
     * @param pd 请求数据
     * @return ObjResult
     */
    @PostMapping("delete-relation")
    @Transactional
    @LogOperation("工作流-删除用户组")
    public ObjResult deleteRelation(@RequestBody PageData pd) {
        userGroupRelationService.deleteByGroupId(pd);
        return new ObjResult(CommonCode.SUCCESS);
    }

    /**
     * 更新成最新的关联信息
     *
     * @param pd 请求数据
     * @return ObjResult
     */
    @PostMapping("update-relation")
    @Transactional
    @LogOperation("删除岗位类型-更新成最新的关联信息")
    public ObjResult updateRelation(@RequestBody PageData pd) {
        userGroupRelationService.deleteByGroupId(pd);
        User user = SessionHelper.getUser();
        String groupId = pd.getString("groupId");
        String groupName = pd.getString("groupName");
        String organizationId = pd.getString("organizationId");
        List<String> userIdList = (List<String>) pd.get("userIdList");
        ArrayList<PageData> pageDataList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userIdList)) {
            for (String userId : userIdList) {
                PageData params = new PageData();
                params.put("id", userId);
                PageData userPageData = userService.findbyid(params).getPageData();
                PageData pageData = new PageData();
                pageData.put("id", UuidUtil.get32UUID());
                pageData.put("userId", userId);
                pageData.put("userName", userPageData.getString("name"));
                pageData.put("groupId", groupId);
                pageData.put("groupName", groupName);
                pageData.put("organizationId", organizationId);
                pageDataList.add(pageData);
            }
        } else {
            PageData pageData = new PageData();
            pageData.put("id", UuidUtil.get32UUID());
            pageData.put("userId", "");
            pageData.put("userName", "");
            pageData.put("groupId", groupId);
            pageData.put("groupName", groupName);
            pageData.put("organizationId", organizationId);
            pageDataList.add(pageData);
        }
        userGroupRelationService.addBatch(pageDataList);
        return new ObjResult(CommonCode.SUCCESS);
    }

    /**
     * 根据单位id获取用户组列表
     *
     * @param pd 请求数据
     * @return ListResult
     */
    @PostMapping("select-by-organization-id")
    public ListResult selectByOrganizationId(@RequestBody PageData pd) {
        List<PageData> pageDataList = userGroupRelationService.selectByOrganizationId(pd);
        Map<String, List<PageData>> map = pageDataList.stream().collect(Collectors.groupingBy(pageData -> pageData.getString("group_id")));
        ArrayList<Object> result = new ArrayList<>();
        for (String item : map.keySet()) {
            ArrayList<String> userIdList = new ArrayList<>();
            ArrayList<String> userNameList = new ArrayList<>();
            PageData per = new PageData();
            List<PageData> pageDataList1 = map.get(item);
            for (PageData i:pageDataList1){
                userIdList.add(i.getString("user_id"));
                userNameList.add(i.getString("user_name"));
            }
            per.put("groupName", pageDataList1.get(0).getString("group_name"));
            per.put("groupId", item);
            per.put("userIdList", userIdList);
            per.put("userNameList", userNameList);
            result.add(per);
        }
        ListResult<Object> objectListResult = new ListResult<>(CommonCode.SUCCESS);
        objectListResult.setList(result);
        return objectListResult;
    }

//    /** TODO DEL
//     * 根据用户组id获取用户
//     *
//     * @param pd 请求数据
//     * @return ListResult
//     */
//    @PostMapping("select-userList-by-groupId")
//    public ListResult selectUserListByGroupId(@RequestBody PageData pd) {
//        List<PageData> pageDataList = userGroupRelationService.selectUserListByGroupId(pd);
//        ListResult<PageData> result = new ListResult<>(CommonCode.SUCCESS);
//        result.setList(pageDataList);
//        return result;
//    }

    @RequestMapping("list")
//    @PassToken
    public ListResult list(@RequestBody PageData pd) {
//        List<PageData> groupList = workFlowGroupService.list();
//        ListResult<PageData> result = new ListResult<>(CommonCode.SUCCESS);
//        result.setList(groupList);
        return workFlowGroupService.listPage(pd);
    }
    @RequestMapping("listShow")
    public ListResult listShow(@RequestBody PageData pd) {
//        List<PageData> groupList = workFlowGroupService.list();
//        ListResult<PageData> result = new ListResult<>(CommonCode.SUCCESS);
//        result.setList(groupList);
        return workFlowGroupService.listShowPage(pd);
    }


}
